import ChildrenPageUtils from '@/store/extends'
import {uSelectCheckHistoryData} from './../../../api/index'
import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import Taro from '@tarojs/taro'

class Paramshistory extends ChildrenPageUtils {
  @computed get devId() {
    return this.pageQuery.devId
  }
  @observable searchData = {
    dateRange: ['', '']
  }

  @computed get dateRange() {
    let s = this.searchData.dateRange[0]
      ? this.searchData.dateRange[0] + ' 00:00:00'
      : ''
    let e = this.searchData.dateRange[1]
      ? this.searchData.dateRange[1] + ' 23:59:59'
      : ''
    return [s, e]
  }

  @action.bound
  changeDateRange(s: string, e: string) {
    console.log(s, e)
    this.searchData.dateRange[0] = s
    this.searchData.dateRange[1] = e
    this.dataList = []
    this.resetPagination()
    this.updateHistory()
  }

  @observable dataList: Device.CheckHistoryData[] = []

  @action.bound
  updateHistory() {
    Taro.showLoading({
      title: '加载中...',
      mask: true
    })
    setTimeout(() => {
      Taro.hideLoading()
    }, 1000)
    if (!this.devId) return

    this.closeUpdate()

    Taro.showLoading()

    uSelectCheckHistoryData({
      devId: this.devId,
      startDate: this.dateRange[0],
      endDate: this.dateRange[1],
      page: this.pagination.page,
      size: this.pagination.size
    })
      .then(res => {
        if (this.pagination.page === 1 && !res.data.list.length) {
          Taro.showModal({
            content: '此时间段内无历史记录'
          })
        }
        this.dataList.push(...res.data.list)
        this.updatePagination({
          page: res.data.page,
          total: res.data.total,
          totalPage: res.data.totalPage
        })
        this.openUpdate()
        Taro.hideLoading()
      })
      .catch(() => {
        this.openUpdate()
        this.delPage()
        Taro.hideLoading()
      })
  }

  @action.bound
  initPageData() {
    this.dataList = []
    this.searchData.dateRange = ['', '']
  }
}
export const paramsHistoryStore = createContext(new Paramshistory())
